package com.binguo.thirdPay.hzShiMing;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * RSA签名工具类
 * Created by 刘志强 on 2017/4/24.
 */
public class RSAUtil {

    /**
     * 日志
     */
    private static final Logger logger                    = LoggerFactory.getLogger(RSAUtil.class);

    private static final int    DEFAULT_BUFFER_SIZE       = 8192;

    private static final String SIGN_CHARSET              = "UTF-8";

    private static final String SIGN_TYPE_RSA             = "RSA";

    private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";

    private static final String SIGN_SHA1RSA_ALGORITHMS   = "SHA1WithRSA";

    //测试Key
//    private static final String PRIVATE_KEY               = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCt9RyDYLRmtaEkdeqTOQxUoRkbeuSkLs5IkRhm9SKPwjDZkpaqykkp6vLQPAkfkzMRPh9cUlBAZg/cmnef5LrWs756VxP3LZC8qHTsCoojHpHduPPOpi2gZ3mXONlYlmNYZLx1gH67VKXg/kZSLJA4k95h7zVo0Y91cfUkPUzng+vJBg2kVogMCQkIS9fu5MF46LGmyrrvusDvjYPwN+9cxi5oBhC3J8a4zcPWeO3HN5yzYeHyz8XA42tvpqvczy3Seu3UjaTnQW+Avpa3GlV0Iat0ohLjHjkM7A1ZgmQk5VSJxc+VIGDZz3XbIcPiWSIGjwRwf6YU3gK9/4DsAiUHAgMBAAECggEAWWHaVr73stach+l2sFLdvRZRmkcY+Ct8nti+2xgVvBwhkYm6lOL/mOQ6Azj1zx2JXdC3uG4rAgvvFoDf+m5CelM9Cgt4HMdy2R0NhsMbm1P3yx9fVs0YIJVKaqMxOk17mk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hEf57qUL5ANyrcE9q2z29UM=";

    //上线Key
    private static final String PRIVATE_KEY               = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaX3oN2qZiwPygrsqT6h+XX6s+O2AcB+0+8I0lGax+/J7c90NaTVTWiAjMgVgOP0R3ydM8z6X+quODI0jl3iGAuzshLHXPM2KJJJ8HHE5Mmq/bYZkCTbM0lyUyc1aLsaolgxbKq3Jc7b8+z1eZZmmkUEAYm3gHEnVIrPyqTnQXLlCgJDZ5ca12igoZDkKgUQ3+JP8U5MMnwSE7EYvdJ21vFwUL4dYAWYqCGeoZF7diECWe3AV/k73R9P9Uw+qfntiJsOTAVw+4xgkcFToxEJAdcpYFnY2eWOVcD2NyLUJ17n0/SBXEPA+17eXeoapsgTJewkONOa8APLFgLmU3TTOdAgMBAAECggEAPXIffQA5FjnG1JZ+X93i8TfNS5oKqV2mW1UYeVn8xRb8jOuGzZzWVgYTqoMZb6JTOHMHN79XTUTsBji3PXfrUfg0kwHFatIDOJ9xBT2JFQjFUsdIRhhzak13Ktup6TSwivngyo7vmHmxk+in75FKH3Mp8CGB5x67vE/OymMr5kWU4jp6aUlbOdPmAtEwlHKJLrXESbwu6n2Y3BNK32tfI1s/iFzTQT0Qfq/9ct5zMRpNvaPaTqV36YvLTGgHK8kwlqGEhAyg37Rf8QuDifcrZlnWoY9prqk4ScOJYnuFT4vj1g2ZDA5YOKzsOBuCpietWxwoPQo1b+hk8Sd10WzlwQKBgQDIxyQS9MfOmlAAmt9juhIMx1G6niJMQfnuaTW5WgoukWoWEMAKKAIbq9XsokLjVLodn5gL138sf2hF4H6DKEgnQZIwvmmiublWtlRKVRpx3alfgq9fuJbFi0/rEcLivOuBMGlW0YT2eImNG1LcPMqlVc7Y3iW2YKdp/NE5xtSCdQKBgQDE1PGlv4xJe/aFPI6UasJf3WTBZajYJQg6/OtvuHWGKkH+qsYj34+wCqV1Vv8p/DTQPHzB/QeluZirUgwswzHoOHTMGDaCU/cNI/zVgTeP5XzkW2uG90HPjKSCs1AOPSuypvfgteph2eO4PXOjewAjh7CNeLSs8GQGgSJ+b1N3iQKBgQC7P8ZsC3BBRfdw6P+IIaPt2QJnGk4ActOmPucE621tit62n36Kuk9lAThtDdN18rH1KL+D48HOfTCBYuJ6bdoFColN/nfEdJPexAMOpBoasdDhq57lleC/cbLiF9l63xiAjubfOl5YA4i9WgtILUYNNl7IMT5YvgHEvi1fRgkxdQKBgDo6tPJP5HSo05k+/P8zRiWOHYyJQpYO/oxU3mHCtCHoxnVMmClsrdlqjOBVyE7nachg3JeW/3grm9qy/x8O7+i/Ku5mC1oq9N9SUi3neER9o6D62qim5TU9aj2wRnKv7gmAE7zKlHg2OYP7ygYHEVdxgO2ZU/oFc9HoygeVb5/hAoGAFhHD9iF9K1w0POSuBTRheMrg4NnTLECihyvz8ugKSxvRC0UCUft90hitfgj1IVTm1ecWWTbFSaplRfXFKXhQ8jqEk7dX78XybmKOrjhEjA49uO4D14X1R6ehWlH0yMMbMizipbnir/dpabbMljA0+OsIr9EY9xfaYUGTF4UX90Y=";

    private static final String PUBLIC_KEY                = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjGpnmKByseFngibUJiNIc4/6wfIQHMOHAsUNe1EACJUrFDFat78IqCJZIsYZ91G8He+88rBQL9+zK7DoubJsHPb6JTHE8krdFN1u/oaEDQz/k5YNff89byrAdjsa3g5GcRU4d1/1D1TpkSVk0BTojtAgCgyNfRPqxB95gF+Bsu2LQB1qMojSVoUx7rbSMofexahjqCWBqIryDYskegjNZhcfh0fiIZd/hK7bZDILN+gHhi60H5vvAEe9M84GYo4yszW51qt8Blf2u5SZ0WkiAsh4+AMFdoRpBDP7MGsmWIYLkylSYy2QoSM4nyaEFZXp3qk/zI9k2HSB/XuW8FwFXQIDAQAB";

    //测试人脸公钥
//    private static final String FACE_PRIVATE_KEY          = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAN5m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hhQYsMFTSMNaExXkEpX0ZYwA+hAgapvDj2l8Cr6BLN+bSHWQ9PobZmM/OzruKfORjTWY21AQzMfsTwVrg+C2cZ9u6KzFP/vyZTjql3Sp44MxayPSSjkVNaHb7s3YQk6omZndLjj036USNcJX/15oAmOqQIDAQAB";

    //上线人脸公钥
    private static final String FACE_PUBLIC_KEY           = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqfsEpGqvsDLc1F3S1C5AGNdjPZoj0CX5Tbm46uSgt/bCs5XxH/bBVKUwks7tXMidHpawJoBDrIAtt540Y7c8+eJw1PXWa9G8RcPzG8CCE/RSCstVh2oMZYbpvdYlPY/g7jh3UDt/ZUw7IGXZbjkn1XJuKLEVQ0IWp94Aju8bNlhm9t3YBF9Bf+XHRnbOmky00wKrWOeFQ7Il+CxY4fG69Rw9noU4x3N+S5wt6A0Zf/iwa1a8D4gpWjicxlzHWss4dYNtUgDPuuIbRQJd1Y2ECD1TFXjepOBptTUV0UdcqstLHbQEKX3g4Xd9KBNVIKTpzYzFyQSwQj9NVoKDEt/ChwIDAQAB";
    //上线人脸私钥
    private static final String FACE_PRIVATE_KEY          = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANm3IenUgotaRyDVWkJvSHn/t2PMWDtwISo3o9X1JvKVVCxDG8JKExJC4X1yWzIagCAqxAncfIuP99RREzY07/epx25CF9NpL4+xQRDukc/qmnQ87QZcqeL5RdMyLCMGm7W/WP8rZ3cysqLp4/z3ADIMrOET/Ax401rocXTgHkY9AgMBAAECgYEAyhhf57G3eGnV6tmBjnfEfFLuRjpykMIWVx0M/Vgyzi3l06x7wQ+HwoTI0ezsUIce9j0cetbr2ZVLhQ+5WLEtKi6aKrH4twxC9pzfTpMdfhNmJIvFk4GYBvch/7h53R64rTsMIkVItq5DG+Aq79ZzpIItbEoQq5xboOqm0lpxNtUCQQD62GZhJPgV0LubqI4G6hUtHjd6qQz5CPxww34cLhjX8aaEQ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ZMZIiuVWY2eDODLRB9mO1UQJBAJN99n7gjlipNGBdna5jFUlzxhW66lkEram3+qU+Vsqy/SzAitEInaO68m6plc+O08AyfYSAGh10sQFSIELC1+ECQQCZ/L3cZ7x28i6liO45imBO4QgIBVBfDCLJgFRNjbMtUrOEWXV6u1MQYgnhkJl9VTmb1woBdy8NxP9qraIkkiB2";

    private static final String RECHARGE_PUBLIC_KEY       = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCekwOD9TU5HlsNx5bOqDyl1IFC8rDvp7Q5ZVMky80fMDdYyxBA0M8Fop7vrIUvF8XiYxni57NraeDgHDudEfIEX5GCAuzR7IWCyjEsqZf69U29gTIxqN8ZxItP3cYB+3yAtvKDeE30Lzi0LxzmqQiEc9YvrxVWGhjrrK+ZvfUMDQIDAQAB";
    /**
     * RSA2签名
     *
     * @param map 待签名数据和签名字段字符串
     * @return 签名
     * @throws Exception 异常上层处理
     */
    public static String rsaSign(Map<String, String> map) throws Exception {
        String content = getSignContent(map);
        String reqSeq = map.get("reqSeq");
        logger.info(">>待签名数据为:" + reqSeq + "," + content);
        PrivateKey priKey = getPrivateKeyFromPKCS8(
            new ByteArrayInputStream(PRIVATE_KEY.getBytes()));
        Signature signature = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
        signature.initSign(priKey);
        signature.update(content.getBytes(SIGN_CHARSET));
        String sign = new String(Base64.encodeBase64(signature.sign()));
        logger.info(">>签名为:" + reqSeq + "," + sign);
        return sign;
    }

    /**
     * RSA2人脸签名
     *
     * @param signString 待签名数据和签名字段字符串
     * @return 签名
     * @throws Exception 异常上层处理
     */
    public static String rsaRechargeSign(String signString) throws Exception {
        logger.info(">>待签名数据为:" + signString);
        PrivateKey priKey = getPrivateKeyFromPKCS8(
                new ByteArrayInputStream(RECHARGE_PRIVATE_KEY.getBytes()));
        Signature signature = Signature.getInstance(SIGN_SHA1RSA_ALGORITHMS);
        signature.initSign(priKey);
        signature.update(signString.getBytes(SIGN_CHARSET));
        String sign = new String(Base64.encodeBase64(signature.sign()));
        logger.info(">>签名为:" + sign);
        return sign;
    }

    /**
     * RSA2人脸签名
     *
     * @param signString 待签名数据和签名字段字符串
     * @return 签名
     * @throws Exception 异常上层处理
     */
    public static String rsaFaceSign(String signString) throws Exception {
        logger.info(">>待签名数据为:" + signString);
        PrivateKey priKey = getPrivateKeyFromPKCS8(
                new ByteArrayInputStream(FACE_PRIVATE_KEY.getBytes()));
        Signature signature = Signature.getInstance(SIGN_SHA1RSA_ALGORITHMS);
        signature.initSign(priKey);
        signature.update(signString.getBytes(SIGN_CHARSET));
        String sign = new String(Base64.encodeBase64(signature.sign()));
        logger.info(">>签名为:" + sign);
        return sign;
    }

    /**
     * RSA2验签
     *
     * @param map 待验签数据、签名信息和签名字段字符串
     * @return 验签结果 true/false
     * @throws Exception 异常上层处理
     */
    public static boolean rsaCheck(Map<String, String> map) throws Exception {
        String content = getSignContent(map);
        String reqSeq = map.get("reqSeq");
        String sign = map.get("sign");
        logger.info(">>待验证的签名为:" + reqSeq + "," + sign);
        logger.info(">>生成签名的参数为:" + reqSeq + "," + content);
        PublicKey pubKey = getPublicKeyFromX509(new ByteArrayInputStream(PUBLIC_KEY.getBytes()));
        Signature signature = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
        signature.initVerify(pubKey);
        signature.update(content.getBytes(SIGN_CHARSET));
        boolean signResult = signature.verify(Base64.decodeBase64(sign.getBytes()));
        logger.info(">>验签结果:" + reqSeq + "," + signResult);
        return signResult;
    }

    /**
     * RSA2验签
     *
     * @param  signString 待验签数据
     * @param  sign 待验签数据
     * @return 验签结果 true/false
     * @throws Exception 异常上层处理
     */
    public static boolean rsaRechargeCheck(String signString,String sign) throws Exception {
        logger.info(">>待验证的签名为:" + signString);
        logger.info(">>生成签名的参数为:"  + sign);
        PublicKey pubKey = getPublicKeyFromX509(new ByteArrayInputStream(RECHARGE_PUBLIC_KEY.getBytes()));
        Signature signature = Signature.getInstance(SIGN_SHA1RSA_ALGORITHMS);
        signature.initVerify(pubKey);
        signature.update(signString.getBytes(SIGN_CHARSET));
        boolean signResult = signature.verify(Base64.decodeBase64(sign.getBytes()));
        logger.info(">>验签结果:" + signResult);
        return signResult;
    }

    public static boolean rsaFaceCheck(String signString,String sign) throws Exception {
        logger.info(">>待验证的签名为:" + signString);
        logger.info(">>生成签名的参数为:"  + sign);
        PublicKey pubKey = getPublicKeyFromX509(new ByteArrayInputStream(FACE_PUBLIC_KEY.getBytes()));
        Signature signature = Signature.getInstance(SIGN_SHA1RSA_ALGORITHMS);
        signature.initVerify(pubKey);
        signature.update(signString.getBytes(SIGN_CHARSET));
        boolean signResult = signature.verify(Base64.decodeBase64(sign.getBytes()));
        logger.info(">>验签结果:" + signResult);
        return signResult;
    }

    /**
     * 把参数按照ASCII码递增排序，如果遇到相同字符则按照第二个字符的键值ASCII码递增排序
     * 将排序后的参数与其对应值，组合成“参数=参数值”的格式，并且把这些参数用&字符连接起来
     *
     * @param sortedParams 待签名数据和签名字段字符串
     * @return 待签名字符串
     */
    public static String getSignContent(Map<String, String> sortedParams) {
        //appId,method,bizContent,生成签名所需的参数
        String[] sign_param = sortedParams.get("sign_param").split(",");
        List<String> keys = new ArrayList<>();
        Collections.addAll(keys, sign_param);
        Collections.sort(keys);
        StringBuilder content = new StringBuilder();
        int index = 0;
        for (String key : keys) {
            String value = sortedParams.get(key);
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)) {
                content.append(index == 0 ? "" : "&").append(key).append("=").append(value);
                index++;
            }
        }
        return content.toString();
    }

    private static PrivateKey getPrivateKeyFromPKCS8(InputStream ins) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(SIGN_TYPE_RSA);
        byte[] encodedKey = readText(ins).getBytes();
        encodedKey = Base64.decodeBase64(encodedKey);
        return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
    }

    private static PublicKey getPublicKeyFromX509(InputStream ins) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(SIGN_TYPE_RSA);
        StringWriter writer = new StringWriter();
        io(new InputStreamReader(ins), writer, -1);
        byte[] encodedKey = writer.toString().getBytes();
        encodedKey = Base64.decodeBase64(encodedKey);
        return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
    }

    private static String readText(InputStream ins) throws IOException {
        Reader reader = new InputStreamReader(ins);
        StringWriter writer = new StringWriter();
        io(reader, writer, -1);
        return writer.toString();
    }

    private static void io(Reader in, Writer out, int bufferSize) throws IOException {
        if (bufferSize == -1) {
            bufferSize = DEFAULT_BUFFER_SIZE >> 1;
        }
        char[] buffer = new char[bufferSize];
        int amount;
        while ((amount = in.read(buffer)) >= 0) {
            out.write(buffer, 0, amount);
        }
    }

    public static void main(String[] args) {
        try {
            RSAUtil.rsaRechargeSign("2010200000000012YunPeng1573526960002{\"cardkind\":\"4\",\"chmchntid\":\"330120400013\",\"iccardno\":\"\",\"cardnumber\":\"676187215\",\"txnamt\":\"10000\"}");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
